<?php 

class Index extends CI_Controller{

	public function __construct()
	{
		parent::__construct();
		//登录校验
		$this->common->check_login();

		$this->user_info = $this->user->get_user_info_id($_SESSION['user']['id']);
	}

	//个人中心首页
	public function index()
	{
		//var_dump(range(2,2));
		$this->load->model ( 'notice_model' );

		$this->load->model('order_model');
		//出场金额校验
		$this->check_out_money();

		$data['result'] = $this->notice_model->notice_list ();

		$data['lv'] = $this->common->get_user_level($this->user_info['score']);
		
		$team_lv_list = $this->common->team_lv_rate();

		$data['team_lv'] = $team_lv_list[$this->user_info['team_lv']];
		
		$sql = "SELECT * FROM pc_ddth_aixinchi WHERE id = '1'";
		$query = $this->db->query ( $sql );
		$query = $query->row_array ();
		
		$data['aixinchi'] = $query['aixinchi'];
		
		
		$data['left'] = $this->load->view('left', '', TRUE);

		$data['menu'] = $this->load->view('menu', '', TRUE);

		$data['user'] = $this->user->get_user_info_id($_SESSION['user']['id']);

		$data['offer'] = $this->order_model->offer_order_row_id($this->user_info['id'],1);
		//var_dump($data['offer']);
		$data['accept'] = $this->order_model->accept_order_row_id($this->user_info['id'],1);
		//echo '<br>';
		//var_dump($data['accept'] );
		$this->load->view('index',$data);
	}

	/**
	 * 获取得到各种利率的出场后的静态金额
	 */
	private function check_out_money()
	{
		//获取自己未出场的记录
		$cc_list = $this->order_model->get_order_log_chuchang($this->user_info['id']);
		//var_dump($cc_list);
		if(!empty($cc_list))
		{
			//循环记录
			foreach ($cc_list as $k => $v) {
				//判断是否存在
				$out_log = $this->order_model->get_out_log($v['id']);
				//var_dump($out_log);
				//订单日志唯一
				if(!empty($out_log))
				{
					continue;
				}
				//echo time().'--';
				//付款日期+设定出场日期等于允许出场日期
				$out_time = $v['dakuan_time'] + ($v['out_day_num'] * 86400);
				//echo $out_time;
				//var_dump(date('Y-m-d H:i:s',$out_time));
				/*出场时间达到，
				*匹配利率（0-6*1%）+
				*出场利率（按积分算等级/按等级算利率，4个等级，1-5天出场日期总和算利率）+
				*出场期额外等级利率中级0.5%、高级1%、顶级1.5%的每日收益 ***作废
				*/
				if(time() >= $out_time)
				{
					$user_lv = $this->common->get_user_level_info($v['user_lv']);
					//匹配利率
					$data['pipei_rate'] = $v['day_num'] * 1;
					//$data['pipei_rate'] = 0 * 1;
					//出场利率计算上限利率
					$max_rate = $user_lv['first_day_rate'] + ($v['out_day_num'] - 1)*0.5;

					$data['out_rate']   = array_sum(range($user_lv['first_day_rate'],$max_rate,0.5));
					//echo $user_lv['first_day_rate'] ;
					//额外等级利率
					//$data['level_rate'] = $user_lv['lv_day_rate'] * $v['out_day_num'];

					$data['old_money']  = $v['money'];

					//$rate = ($data['pipei_rate'] + $data['out_rate'] + $data['level_rate'])/100;

					$rate = ($data['pipei_rate'] + $data['out_rate'])/100;

					$data['new_money']  = ($v['money'] * $rate);

					$data['ol_id']      = $v['id'];

					$data['user_id']    = $this->user_info['id'];
					
					
					// 申请时间 到出场时间
					$data['daynum'] = (time() - $v['create_time'])/86400;
					
					//插入
					$this->order_model->out_log_insert($data);
					//更新订单日志出场状态
					$this->order_model->order_log_update_chuchang($v['id']);
					
					//更新静态金额
					$jt_money = $v['money'] + $data['new_money'];
					$this->user->update_user_jt_money($jt_money,$this->user_info['id']);
					//添加出场记录，更新订单日志状态，更新用户静态金额
				}
			}
		}
		//未出场略过
	}

}